<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>用户信息</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
    <link rel="shortcut icon" href="../../static/favicon.ico" type="image/x-icon" />
    <link rel="stylesheet" href="../../static/user/css/font.css">
    <link rel="stylesheet" href="../../static/user/css/font.css">
    <link rel="stylesheet" href="../../static/user/css/xadmin.css">
    <script type="text/javascript" src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
    <script type="text/javascript" src="../../static/user/lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="../../static/user/js/xadmin.js"></script>
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>

<body>
<div class="x-nav">
      <span class="layui-breadcrumb">
        <a href="#">用户列表</a>
          <a href="/dish/dishUI">点菜界面</a>
      </span>
    <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right"
       href="javascript:location.replace(location.href);" title="刷新">
        <i class="layui-icon" style="line-height:30px">ဂ</i></a>
</div>
<div class="x-body">
    <div class="layui-row">
        <form class="layui-form layui-col-md12 x-so">
            <input type="text" placeholder="请输入用户名或姓名查询" id="queryUser" autocomplete="off" class="layui-input">
            <button class="layui-btn" type="button" onclick="queryByName()"><i class="layui-icon">&#xe615;</i></button>
        </form>
    </div>

    <xblock>
        <button class="layui-btn" onclick="x_admin_show('添加信息' ,'userAddUI', 800, 600)"><i class="layui-icon"></i>添加
        </button>
        <span class="x-right" style="line-height:40px" id="showTotal"></span>
    </xblock>
    <table class="layui-table">
        <thead>
        <tr>
            <th>序号</th>
            <th>用户名</th>
            <th>密码</th>
            <th>昵称</th>
            <th>姓名</th>
            <th>性别</th>
            <th>电话</th>
            <th>身份证号码</th>
            <th>用户状态</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody id="tab"></tbody>
    </table>


</div>
<script>
    $(document).ready(
        init()
    )

    function selectSalary() {
        init();
    }

    function init () {
        //    初始化操作
        selectAll();
    }

    //     查询全部用户
    function selectAll() {
        //    做ajax请求
        $.ajax({
            url:'list',
            dataType:'json',
            type:'post',
            //同步
            async:false,
            success:function (data) {
                console.log(data);
                let userList = data.userList;
                $('#showTotal').text('共有数据：' + userList.length + '条');
                if(userList.length > 0){
                    let str = '';//用str变量来拼接用户数据
                    for(let i = 0; i < userList.length; i++){
                        str += '<tr>'+
                            '<td>'+ (i + 1) + '</td>' +
                            '<td>'+ userList[i].username + '</td>' +
                            '<td>'+ userList[i].password + '</td>' +
                            '<td>'+ userList[i].nikename + '</td>' +
                            '<td>'+ userList[i].name + '</td>' +
                            '<td>'+ userList[i].sex + '</td>' +
                            '<td>'+ userList[i].tel + '</td>' +
                            '<td>'+ userList[i].idCard + '</td>' +
                            '<td>'+ userList[i].userState + '</td>' +
                            '<td class="td-maager">' +
                            '<button type="button" onclick="updateUser(this, '+ "'" + userList[i].uid + "'" + ')"><i class="layui-icon">&#xe642;</i></button>' +
                            '<button type="button" onclick="delUser('+ "'" + userList[i].uid + "'" + ')"><i class="layui-icon">&#xe640;</i></button>' +
                            // '<button type="button" onclick="checkSalary('+ "'" + salaryList[i].username + "'" + ')"><i>奖罚详情</i></button>' +
                            '</td>' +
                            '</tr>'
                    };
                    //html().append()
                    $('#tab').html(str);
                }else{
                    //    无数据

                    $('#tab').html('<tr><td colspan="8" align="center">没有数据</td></tr>');
                }
            },error:function () {
                alert("服务器错误！");
            }
        })
    }

    //     查询全部用户
    function queryByName() {
        //    做ajax请求
        $.ajax({
            url:'queryByName',
            dataType:'json',
            type:'post',
            data: {
                name: $("#queryUser").val()
            },
            //同步
            async:false,
            success:function (data) {
                console.log(data);
                let userList = data.userList;
                $('#showTotal').text('共有数据：' + userList.length + '条');
                if(userList.length > 0){
                    let str = '';//用str变量来拼接用户数据
                    for(let i = 0; i < userList.length; i++){
                        str += '<tr>'+
                            '<td>'+ (i + 1) + '</td>' +
                            '<td>'+ userList[i].username + '</td>' +
                            '<td>'+ userList[i].password + '</td>' +
                            '<td>'+ userList[i].nikename + '</td>' +
                            '<td>'+ userList[i].name + '</td>' +
                            '<td>'+ userList[i].sex + '</td>' +
                            '<td>'+ userList[i].tel + '</td>' +
                            '<td>'+ userList[i].idCard + '</td>' +
                            '<td>'+ userList[i].userState + '</td>' +
                            '<td class="td-maager">' +
                            '<button type="button" onclick="updateUser(this, '+ "'" + userList[i].uid + "'" + ')"><i class="layui-icon">&#xe642;</i></button>' +
                            '<button type="button" onclick="delUser('+ "'" + userList[i].uid + "'" + ')"><i class="layui-icon">&#xe640;</i></button>' +
                            // '<button type="button" onclick="checkSalary('+ "'" + salaryList[i].username + "'" + ')"><i>奖罚详情</i></button>' +
                            '</td>' +
                            '</tr>'
                    };
                    //html().append()
                    $('#tab').html(str);
                }else{
                    //    无数据

                    $('#tab').html('<tr><td colspan="8" align="center">没有数据</td></tr>');
                }
            },error:function () {
                alert("服务器错误！");
            }
        })
    }

    function delUser(id) {
        if (confirm("是否真的要删除该用户?")) {
            $.ajax({
                url:'del/' + id,
                dataType: 'json',
                type: 'post',
                success: function (data) {
                    let res = data.code;
                    console.log(res);
                    if(res === 200) {
                        alert("删除成功！");
                    } else {
                        alert("删除失败！");
                    }
                }, error: function () {
                    alert("发送失败")
                }
            })
            location.reload();
        }
    }

    function updateUser(obj, uid) {
        let username = $(obj).parent().parent().find('td').eq(1).text();
        let password = $(obj).parent().parent().find('td').eq(2).text();
        let nickname = $(obj).parent().parent().find('td').eq(3).text();
        let name = $(obj).parent().parent().find('td').eq(4).text();
        let sex = $(obj).parent().parent().find('td').eq(5).text();
        let tel = $(obj).parent().parent().find('td').eq(6).text();
        let idCard = $(obj).parent().parent().find('td').eq(7).text();
        let userState = $(obj).parent().parent().find('td').eq(8).text();

        // 把数据存入localStorage对象中
        localStorage.setItem("uid", uid);
        localStorage.setItem("updateUsername", username);
        localStorage.setItem("updatePassword", password);
        localStorage.setItem("updateNickname", nickname);
        localStorage.setItem("updateName", name);
        localStorage.setItem("updateSex", sex);
        localStorage.setItem("updateTel", tel);
        localStorage.setItem("updateIdCard", idCard);
        localStorage.setItem("updateUserState", userState);
        // 打开修改页面
        x_admin_show('修改用户', 'updateUI', 900, 600);
    }

</script>

</body>
</html>